﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Web.Http;
using System.Web;
using System.Diagnostics;
using System.Net.Http;
using Microsoft.Xaml.Behaviors.Media;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Collections.Generic;

namespace MotionControl.Modules.RestService.business
{
    public class ClientInfo
    {
        public string Name { get; set; }
        public string IP { get; set; }
        public bool IsSelected { get; set; } = false;
    }
    public class HttpService
    {
        public static HttpService GetInstance { get => _instance.Value; }

        private static readonly Lazy<HttpService> _instance = new Lazy<HttpService>(() => new HttpService(), true);
        private HttpService() { }
        ~HttpService() {
            _tokenSource.Cancel();
        }

        private CancellationTokenSource _tokenSource = new CancellationTokenSource();
        public Action<string, string> PLCMsgAction { get; set; }
        public Action<string, string> LightMsgAction { get; set; }
        public Action<string, string> HttpMsgAction { get; set; }

        private WebApplication _webApp;
        public void Start(string url)
        {
            try{
                var builder = WebApplication.CreateBuilder();

                builder.Services.AddControllers();

                builder.Services.AddEndpointsApiExplorer();

                _webApp = builder.Build();

                _webApp.UseHttpsRedirection();

                _webApp.Urls.Add(url);

                //_webApp.MapPost("/plc", (HttpContext context) => PLCMsgHandler(context));
                //_webApp.MapPost("/light", (HttpContext context)=>LightMsgHandler(context));
                _webApp.MapPost("", (HttpContext context) => HttpMsgHandler(context));
                _webApp.MapControllers();
                _webApp.RunAsync(_tokenSource.Token);
            }
            catch (Exception ex) { 

            }       
        }
        public void AddEndpoint(string url)
        {
            _tokenSource.Cancel();
            Start(url);
        }

        private void HttpMsgHandler(HttpContext context)
        {
            var ipStr = context.Connection.RemoteIpAddress.MapToIPv4().ToString();
            var para = context.Request.Form["data"];
            HttpMsgAction?.Invoke(ipStr,para);
        }

        private void PLCMsgHandler(HttpContext context)
        {
            var clientIp = context.Connection.RemoteIpAddress;
            var para = context.Request.Form["data"];
            PLCMsgAction?.Invoke(clientIp.ToString(),para);
        }
        private void LightMsgHandler(HttpContext context)
        {
            var clientIp = context.Connection.RemoteIpAddress;
            var para = context.Request.Form["data"];
            LightMsgAction?.Invoke(clientIp.ToString(), para);
        }
    }
}
